package com.hsogoo.study.concurrency;

import java.util.concurrent.locks.LockSupport;

import org.junit.Test;

/**
 * Created by za-huangsaigang on 2018/9/5.
 */
public class LockSupportTest {

    @Test
    public void testLockSupport() throws InterruptedException{
        Thread thread = new MyThread();
        thread.start();

        Thread.sleep(5000L);
        System.out.println(thread.getState());
        LockSupport.unpark(thread);
//        thread.interrupt(); 中断线程会让park()返回
        System.out.println("unpack MyThread...");
        thread.join();
    }

    public class MyThread extends Thread{

        @Override
        public void run(){
            for (int i = 0; i < 20; i++) {
                try {
                    sleep(100L);
                    System.out.println(i);
                    if(i == 10){
                        System.out.println("pack MyThread...");
                        LockSupport.park();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("finished...");
        }
    }

}
